package com.imooc.reader.management.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.imooc.reader.management.entity.Book;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Map;


public interface BookMapper extends BaseMapper<Book> {
    @Select("select b.book_id bookId,b.book_name bookName , b.sub_title subTitle ,b.author , b.cover , b.description , b.category_id categoryId , \n" +
            "       b.evaluation_score evaluationScore , b.evaluation_quantity evaluationQuantity , c.category_name categoryName  \n" +
            "from book b,category c where b.category_id = c.category_id")
    IPage<Map> selectBookMap(IPage page);

    @Update("""
        update book b set evaluation_score = (
                    select ifnull(avg(score),0) from evaluation e where e.book_id = b.book_id and state = 'enable'
                ),evaluation_quantity = (
                    select ifnull(count(*),0) from evaluation e where e.book_id = b.book_id and state = 'enable'
                )
    """)
    void updateScore();
}
